package com.devunion.reader.client.panel;

import com.devunion.reader.client.core.AbstractComposite;
import com.devunion.reader.client.to.AnnotationTo;
import com.devunion.reader.client.util.RequestUtil;
import com.extjs.gxt.ui.client.Style;
import com.extjs.gxt.ui.client.fx.Draggable;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.google.gwt.user.client.ui.Image;

import java.util.Collection;

/**
 * @author Timoshenko Alexander 19.08.2009 : 20:04:23
 */
public class ImageViewPanel extends AbstractComposite {
    private String imagePath;
    private String projectName;
    private Collection<AnnotationTo> annotations;

    public void setImagePath(String imagePath) {
        this.imagePath = imagePath;
    }

    public void setAnnotations(Collection<AnnotationTo> annotations) {
        this.annotations = annotations;
    }

    public void setProjectName(String projectName) {
        this.projectName = projectName;
    }

    public void initialize() {
        setScrollMode(Style.Scroll.AUTO);
        setFrame(true);
        setMonitorResize(true);
        setMonitorWindowResize(true);
    }

    public void build() {
        LayoutContainer imageContainer = new LayoutContainer();
        imageContainer.setAutoHeight(true);
        imageContainer.setAutoWidth(true);
        imageContainer.add(new Image(RequestUtil.getContextPath() + "loadimage.rpc?pname=" +
                projectName + "&path=" + imagePath));
        add(imageContainer);
        layout();

        int offset = 0;
        if (annotations != null) {
            for (AnnotationTo annotationTo : annotations) {
                LayoutContainer annotation = new LayoutContainer();

                annotation.setWidth(annotationTo.getWidth());
                annotation.setHeight(annotationTo.getHeight());
                annotation.setPosition(annotationTo.getX(), annotationTo.getY() - imageContainer.getHeight() - offset);
                annotation.addText(annotationTo.getText());
                add(annotation);
                offset += annotationTo.getHeight();
            }
        }
    }

    @Override
    protected void onWindowResize(int i, int i1) {
        super.onWindowResize(i, i1);
        removeAll();
        if (imagePath != null) {
            build();
            layout();
        }
    }
}
